package com.csun.dsms.controller;

import com.csun.dsms.comment.core.annotation.Authorization;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.model.domain.YcAlert;
import com.csun.dsms.model.domain.YcDevice;
import com.csun.dsms.model.domain.YcDeviceData;
import com.csun.dsms.model.dto.yc.YcDataDto;
import com.csun.dsms.model.dto.yc.YcDataJinDto;
import com.csun.dsms.model.dto.yc.YcDeviceQueryDto;
import com.csun.dsms.service.YcAlertService;
import com.csun.dsms.service.YcDeviceDataService;
import com.csun.dsms.service.YcDeviceService;
import com.csun.dsms.utils.PublicUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/yc")
public class YcDeviceController {

    @Resource
    private YcDeviceService ycDeviceServiceImpl;
    @Resource
    private YcDeviceDataService ycDeviceDataServiceImpl;

    @Resource
    private YcAlertService ycAlertServiceImpl;


    /*
    * 设备的添加
    * */
    @Authorization
    @RequestMapping(value = "/deviceAdd",method = RequestMethod.POST)
    public Wrapper deviceAdd (@RequestBody YcDevice ycDevice){
        if(PublicUtil.isEmpty(ycDevice.getNo().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"设备编号不为空");
        }
        YcDevice ycQuery = new YcDevice();
        ycQuery.setNo(ycDevice.getNo());
        List<YcDevice> query = ycDeviceServiceImpl.query(ycQuery);
        if(query.size()!=0){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"设备编号已存在");
        }
        ycDevice.setId(null);
        Integer save = ycDeviceServiceImpl.save(ycDevice);
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }


    /*
     * 设备的添加
     * */
    @Authorization
    @RequestMapping(value = "/deviceQuery",method = RequestMethod.POST)
    public Wrapper deviceQuery (@RequestBody YcDevice ycDevice){
        YcDevice ycQuery = ycDevice;
        List<YcDevice> query = ycDeviceServiceImpl.query(ycQuery);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE,new PageInfo<>(query));
    }

    /*
     * 设备数据的添加
     * */
    @Authorization
    @RequestMapping(value = "/upload",method = RequestMethod.POST)
    public Wrapper dataAdd(@RequestBody YcDataJinDto ycDataJinDto){
        if(PublicUtil.isEmpty(ycDataJinDto.getDeviceId().trim())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"设备编号不为空");
        }
        YcDeviceData ycDeviceData = new YcDeviceData();
        ycDeviceData.setNo(ycDataJinDto.getDeviceId());
        ycDeviceData.setType(0); //金秋实的
        ycDeviceData.setPm25(ycDataJinDto.getPm25());
        ycDeviceData.setPm10(ycDataJinDto.getPm10());
        ycDeviceData.setNoise(ycDataJinDto.getNoise());
        ycDeviceData.setTem(ycDataJinDto.getTem());
        ycDeviceData.setHum(ycDataJinDto.getHum());
        ycDeviceData.setWp(ycDataJinDto.getWp());
        ycDeviceData.setWs(ycDataJinDto.getWs());
        ycDeviceData.setWd360(ycDataJinDto.getWd());
        ycDeviceData.setTsp(ycDataJinDto.getTsp());
        ycDeviceData.setAtm(ycDataJinDto.getAtm());
        ycDeviceData.setCreatedTime(ycDataJinDto.getDatatime());
        Integer save = ycDeviceDataServiceImpl.save(ycDeviceData);
        if(save<1){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,Wrapper.ERROR_MESSAGE);
        }
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE,Wrapper.SUCCESS_MESSAGE);
    }


    @Authorization
    @RequestMapping(value = "/queryHistory",method = RequestMethod.POST)
    public Wrapper queryHistory(@RequestBody YcDeviceQueryDto ycDeviceQueryDto){
        if(PublicUtil.isEmpty(ycDeviceQueryDto.getDeviceId()) &&
                PublicUtil.isEmpty(ycDeviceQueryDto.getNo())){
            return WrapMapper.wrap(Wrapper.ERROR_CODE,"设备编号不为空");
        }
        List<YcDeviceData> dataList = ycDeviceDataServiceImpl.query(ycDeviceQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作成功", new PageInfo(dataList));
    }

    //realData
    // 获取多个设备的
    @Authorization
    @RequestMapping(value = "/realData",method = RequestMethod.POST)
    public Wrapper realData(@RequestBody YcDeviceQueryDto ycDeviceQueryDto) {
        List<YcDataDto> dataList = ycDeviceDataServiceImpl.realData(ycDeviceQueryDto);
        dataList.forEach(item->{
            long minute = (new Date().getTime() - item.getCreatedTime().getTime()) / (60 * 1000);
            if (minute > 15){
                item.setStatus("offline");
                item.setAlert("设备离线报警");
            }else{
                item.setStatus("online");
            }
            if (item.getPm25()>80){
                item.setAlert("PM2.5超上限报警");
            }else if (item.getPm10()>70){
                item.setAlert("PM10超上限报警");
            }else if (item.getTsp()>80){
                item.setAlert("TSP浓度超上限报警");
            }else if (item.getNoise()>70){
                item.setAlert("噪音超标报警");
            }
        });
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作成功", dataList);
    }

    @Authorization
    @RequestMapping(value = "/queryAlert",method = RequestMethod.POST)
    public Wrapper queryAlert(@RequestBody YcDeviceQueryDto ycDeviceQueryDto){
        List<YcAlert> dataList = ycAlertServiceImpl.query(ycDeviceQueryDto);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作成功", new PageInfo(dataList));
    }

    @Authorization
    @RequestMapping(value = "/readAlert",method = RequestMethod.POST)
    public Wrapper readAlert(@RequestBody YcAlert ycAlert){
        Integer result = ycAlertServiceImpl.readAlert(ycAlert);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, "操作成功", result);
    }

}
